#include "systick.h"

static uint8_t fac_us = 0;  // ???????
static uint16_t fac_ms = 0; // ???????

void SysTick_Init(void) {
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);  // ??????(HCLK/8)
    fac_us = SystemCoreClock / 8000000;  // ???????(??????72MHz)
    fac_ms = (uint16_t)fac_us * 1000;    // ?????
}

void SysTick_DelayMs(uint32_t ms) {
    uint32_t temp;
    SysTick->LOAD = ms * fac_ms;         // ?????
    SysTick->VAL = 0x00;                 // ?????
    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;  // ????
    do {
        temp = SysTick->CTRL;
    } while ((temp & 0x01) && !(temp & (1 << 16)));  // ??????
    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;  // ?????
    SysTick->VAL = 0x00;                       // ?????
}